<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>OpenCV环境搭建 | ChenBlue | 飞雪连天射白鹿 笑书神侠倚碧鸳</title>

  
  <meta name="author" content="ChenBlue">
  

  
  <meta name="description" content="学习笔记">
  

  
  
  <meta name="keywords" content="环境搭建,OpenCV">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="OpenCV环境搭建"/>

  <meta property="og:site_name" content="ChenBlue"/>

  
  <meta property="og:image" content="/blog/favicon.ico"/>
  

  <link href="/blog/favicon.ico" rel="icon">
  <link rel="alternate" href="/blog/atom.xml" title="ChenBlue" type="application/atom+xml">
  <link rel="stylesheet" href="/blog/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/blog/">ChenBlue</a>
    </h1>
    <p class="site-description">飞雪连天射白鹿 笑书神侠倚碧鸳</p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>OpenCV环境搭建</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/blog/2021/07/25/OpenCV环境搭建/" rel="bookmark">
        <time class="entry-date published" datetime="2021-07-25T09:05:03.000Z">
          2021-07-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/f646448da265">文章转载简友博客</a></p>
<h1 id="Ubuntu-16-04-安装Opencv-3-4-3"><a href="#Ubuntu-16-04-安装Opencv-3-4-3" class="headerlink" title="Ubuntu 16.04 安装Opencv 3.4.3"></a>Ubuntu 16.04 安装Opencv 3.4.3</h1><h2 id="1-1-下载Opencv-3-4-3"><a href="#1-1-下载Opencv-3-4-3" class="headerlink" title="1.1 下载Opencv 3.4.3"></a>1.1 下载Opencv 3.4.3</h2><p>去官网下载opencv，在本教程中选用的时opencv3.4.3，其他版本的配置方法异曲同工。<br><a target="_blank" rel="noopener" href="http://opencv.org/releases.html">下载</a>，选择sources版本。</p>
<h2 id="1-2-解压zip包"><a href="#1-2-解压zip包" class="headerlink" title="1.2 解压zip包"></a>1.2 解压zip包</h2><pre class="language-none"><code class="language-none">unzip opencv-3.4.3.zip
cd opencv-3.4.3</code></pre>
<h2 id="1-3-安装依赖库和cmake"><a href="#1-3-安装依赖库和cmake" class="headerlink" title="1.3 安装依赖库和cmake"></a>1.3 安装依赖库和cmake</h2><pre class="language-none"><code class="language-none">sudo apt-get install cmake  
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev  </code></pre>
<h2 id="1-4-执行cmake"><a href="#1-4-执行cmake" class="headerlink" title="1.4 执行cmake"></a>1.4 执行cmake</h2><pre class="language-none"><code class="language-none">&#x2F;* 新建编译文件夹*&#x2F;
mkdir my_build_dir
cd my_build_dir
&#x2F;* 执行cmake *&#x2F;
cmake ..</code></pre>
<h2 id="1-5-执行make命令"><a href="#1-5-执行make命令" class="headerlink" title="1.5 执行make命令"></a>1.5 执行make命令</h2><pre class="language-none"><code class="language-none">sudo make  &#x2F;&#x2F; 时间比较漫长</code></pre>
<h2 id="1-6-执行install命令"><a href="#1-6-执行install命令" class="headerlink" title="1.6 执行install命令"></a>1.6 执行install命令</h2><pre class="language-none"><code class="language-none">sudo make install</code></pre>
<p><em>这一步执行完毕之后，Opencv的编译过程就结束了，接下来的工作就是配置一些Opencv的编译环境。</em></p>
<h1 id="2-1-将OpenCV的库添加到路径"><a href="#2-1-将OpenCV的库添加到路径" class="headerlink" title="2.1 将OpenCV的库添加到路径"></a>2.1 将OpenCV的库添加到路径</h1><p><strong>首先将OpenCV的库添加到路径，从而可以让系统找到</strong></p>
<pre class="language-none"><code class="language-none">sudo vi &#x2F;etc&#x2F;ld.so.conf.d&#x2F;opencv.conf </code></pre>
<p><strong>执行此命令后打开的可能是一个空白的文件，不用管，只需要在文件末尾添加</strong></p>
<pre class="language-none"><code class="language-none">&#x2F;usr&#x2F;local&#x2F;lib  </code></pre>
<h2 id="2-2-生效配置文件"><a href="#2-2-生效配置文件" class="headerlink" title="2.2 生效配置文件"></a>2.2 生效配置文件</h2><p>执行如下命令使得刚才的配置路径生效：</p>
<pre class="language-none"><code class="language-none">sudo ldconfig </code></pre>
<h2 id="2-3-配置bash"><a href="#2-3-配置bash" class="headerlink" title="2.3 配置bash"></a>2.3 配置bash</h2><pre class="language-none"><code class="language-none">sudo vi &#x2F;etc&#x2F;bash.bashrc </code></pre>
<p><strong>在末尾追加：</strong></p>
<pre class="language-none"><code class="language-none">PKG_CONFIG_PATH&#x3D;$PKG_CONFIG_PATH:&#x2F;usr&#x2F;local&#x2F;lib&#x2F;pkgconfig  
export PKG_CONFIG_PATH</code></pre>
<p><strong>保存，执行如下命令使得配置生效：</strong></p>
<pre class="language-none"><code class="language-none">source &#x2F;etc&#x2F;bash.bashrc</code></pre>
<p><strong>更新</strong></p>
<pre class="language-none"><code class="language-none">sudo updatedb</code></pre>
<p><em>至此，所有配置都已经完成。</em></p>
<h1 id="3-小程序测试"><a href="#3-小程序测试" class="headerlink" title="3 小程序测试"></a>3 小程序测试</h1><p>找到 opencv-3.4.3/samples/cpp/example_cmake 目录下，官方已经给出了一个cmake的example，我们可以拿来测试下。按顺序执行：</p>
<pre class="language-none"><code class="language-none">cmake .
make
.&#x2F;opencv_example</code></pre>
<p><em>即可看到打开了摄像头，在左上角有一个hello opencv ，即表示配置成功。</em></p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    
      

    <span class="post-categories">
      <i class="icon-categories"></i>
        <a href="/blog/categories/环境搭建/">环境搭建</a>
    </span>
    

    
    

    <span class="post-tags">
      <i class="icon-tags"></i>
        <a href="/blog/tags/环境搭建/">环境搭建</a><a href="/blog/tags/OpenCV/">OpenCV</a>
    </span>
    

    </div>

    
  </div>
</article>

  









    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2023 ChenBlue
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>